/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.ta.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.ta.entity.XtLoupan;
import com.thinkgem.jeesite.modules.ta.service.XtLoupanService;
import com.thinkgem.jeesite.modules.ta.util.GetLouPanInfoUtil;

/**
 * 楼盘展示Controller
 * @author 刘雨轩
 * @version 2017-06-13
 */
@Controller
@RequestMapping(value = "${adminPath}/ta/xtLoupan")
public class XtLoupanController extends BaseController {

	public String ascii2native(String ascii) {
		int n = ascii.length() / 6;
		StringBuilder sb = new StringBuilder(n);
		for (int i = 0, j = 2; i < n; i++, j += 6) {
			String code = ascii.substring(j, j + 4);
			char ch = (char) Integer.parseInt(code, 16);
			sb.append(ch);
		}
		return sb.toString();
	}
	
	@Autowired
	private XtLoupanService xtLoupanService;
	
	@SuppressWarnings("unchecked")
	@RequiresPermissions("ta:xtLoupan:view")
	@RequestMapping(value = {"list", ""})
	public String list(HttpServletRequest request, Model model) {
		List<XtLoupan> lp = xtLoupanService.findList(null);
		if(lp.size() != 0){
			Object[] objs = GetLouPanInfoUtil.getInfo(request);
			int lc = xtLoupanService.findMax();
			List<Integer> louceng = new ArrayList<Integer>();
			for (int i = lc; i > 0; i--) {
				louceng.add(i);
			}
			model.addAttribute("louceng", louceng);
			List<String> lx = (List<String>) objs[0];
			List<String> hx = (List<String>) objs[1];
			model.addAttribute("lp", lp);
			model.addAttribute("lx", lx);
			model.addAttribute("hx", hx);
			model.addAttribute("lc", lc);
		}
		return "modules/ta/xtLoupanList";
	}
	
	@RequiresPermissions("ta:xtLoupan:view")
	@RequestMapping(value = {"show"})
	public String show(HttpServletRequest request, Model model) {
		List<XtLoupan> lp = xtLoupanService.findList(null);
		if(lp.size() != 0){
			@SuppressWarnings("unchecked")
			Map<String, String> map = (Map<String, String>) GetLouPanInfoUtil.getInfo(request)[2];
			int lc = xtLoupanService.findMax();
			List<Integer> louceng = new ArrayList<Integer>();
			for (int i = lc; i > 0; i--) {
				louceng.add(i);
			}
			model.addAttribute("louceng", louceng);
			model.addAttribute("lp", lp);
			model.addAttribute("map", map);
		}
		return "modules/ta/xtLoupanShowList";
	}
	
	
	@RequiresPermissions("ta:xtLoupan:view")
	@RequestMapping(value = "getx",produces="application/json;charset=utf-8")
	public @ResponseBody Object getx(int fh,HttpServletRequest request, Model model) {
		XtLoupan xtloupan = xtLoupanService.get(fh);
		return xtloupan;
	}

	

	@RequiresPermissions("ta:xtLoupan:edit")
	@RequestMapping(value = "insertRoom")
	public String insertRoom(int bottom,int topfloor,int roomnum,RedirectAttributes redirectAttributes) {
		List<XtLoupan> lp = xtLoupanService.findList(null);
		if(lp.size() != 0){
			addMessage(redirectAttributes, "已有楼盘信息，无法生成，请先删除");
			return "redirect:"+Global.getAdminPath()+"/ta/xtLoupan/?repage";
		}
		for (int i = bottom; i <= topfloor; i++) {
			for (int j = 1; j <= roomnum; j++) {
				int romn = 0;
				if(j >= 100){
						romn = Integer.parseInt(i+""+j);
				}else{
					romn = Integer.parseInt(i+"0"+j);
				}
				XtLoupan xlp = new XtLoupan();
				xlp.setLch(i);
				xlp.setFh(romn);
				xtLoupanService.insertRoom(xlp);
			}
		}
		return "redirect:"+Global.getAdminPath()+"/ta/xtLoupan/?repage";
	}
	
	@RequiresPermissions("ta:xtLoupan:edit")
	@RequestMapping(value = "update")
	public String update(String [] str,String fwlx,String fx,int ycjzmj,int scjzmj,RedirectAttributes redirectAttributes) {
		XtLoupan xtLoupan = new XtLoupan();
		xtLoupan.setFwlx(ascii2native(fwlx));
		xtLoupan.setFx(ascii2native(fx));
		xtLoupan.setYcjzmj(ycjzmj);
		xtLoupan.setScjzmj(scjzmj);
		for (String string : str) {
			xtLoupan.setFh(Integer.parseInt(string));
			xtLoupanService.update(xtLoupan);
		}
		addMessage(redirectAttributes, "楼盘批量设置成功");
		return "redirect:"+Global.getAdminPath()+"/ta/xtLoupan/?repage";
	}
	
	@RequiresPermissions("ta:xtLoupan:edit")
	@RequestMapping(value = "upd",produces="application/json;charset=utf-8")
	public @ResponseBody Object upd(int fh,String fwlx,String fx,int ycjzmj,int scjzmj,HttpServletRequest request,RedirectAttributes redirectAttributes) {
		XtLoupan xtLoupan = new XtLoupan();
		xtLoupan.setFwlx(fwlx);
		xtLoupan.setFx(fx);
		xtLoupan.setYcjzmj(ycjzmj);
		xtLoupan.setScjzmj(scjzmj);
		xtLoupan.setFh(fh);
		xtLoupanService.update(xtLoupan);
		return null;
	}

	
	@RequiresPermissions("ta:xtLoupan:edit")
	@RequestMapping(value = "deleteAll")
	public String deleteAll(RedirectAttributes redirectAttributes) {
		xtLoupanService.deleteAll();
		return "redirect:"+Global.getAdminPath()+"/ta/xtLoupan/?repage";
	}
	
	@RequiresPermissions("ta:xtLoupan:edit")
	@RequestMapping(value = "delete")
	public String delete(String [] de,RedirectAttributes redirectAttributes) {
		XtLoupan xtLoupan = new XtLoupan();
		for (String string : de) {
			xtLoupan.setFh(Integer.parseInt(string));
			xtLoupanService.delete(xtLoupan);
		}
		return "redirect:"+Global.getAdminPath()+"/ta/xtLoupan/?repage";
	}

}